# -*- coding: utf-8 -*-
__author__ = 'sysprog'
import os
from django import template
TEMPLATE_DIR = os.path.join(os.path.abspath(os.path.dirname(__file__)), "templates")


def get_template(tmpl_name):
    import codecs
    template_file_name = os.path.join(TEMPLATE_DIR, tmpl_name)

    with codecs.open(template_file_name, "r", encoding="utf-8") as f:
        html = unicode(f.read())

        return template.Template(html)


class MenuTemplate(template.Node):
    def __init__(self, menu_template_name):
        self.mtn = menu_template_name

    def render(self, context):
        tmpl = get_template(self.mtn)
        return tmpl.render(context)

def __render_menu(parser, token):
    try:
        tag_name, format_string = token.split_contents()
    except ValueError:
        raise template.TemplateSyntaxError("%r tag requires a single argument" % token.contents.split()[0])

    if not (format_string[0] == format_string[-1] and format_string[0] in ('"', "'")):
        raise template.TemplateSyntaxError("%r tag's argument should be in quotes" % tag_name)
    return MenuTemplate(format_string[1:-1])

def render_head(title):
    return """<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css">
     <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap-theme.min.css">
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>
    <style>.bordered{box-shadow: 0 0 2px black;}.white-bg{background:#ffffff !important;}</style>
    <link rel="stylesheet" href="http://cdnjs.cloudflare.com/ajax/libs/highlight.js/8.4/styles/default.min.css">
    <script src="http://cdnjs.cloudflare.com/ajax/libs/highlight.js/8.4/highlight.min.js"></script>
    <script>hljs.initHighlightingOnLoad();</script>
    <title>%(title)s</title>
    <meta charset="UTF-8">""" % {"title": title}

register = template.Library()
register.tag("render_menu", __render_menu)
register.simple_tag(render_head)
template.builtins.append(register)


"""
Filters for control 3.5
"""
from django.template.defaulttags import register as reg
@reg.filter
def get_var_name(dictionary, key):
    return dictionary.get(key)["var"].spelling
@reg.filter
def get_var_type(dictionary, key):
    return dictionary.get(key)["var"].type.spelling
@reg.filter
def get_used_in(dictionary, key):
    return dictionary.get(key)["used_in_list"]

